package com.tjing.bussiness.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.tjing.frame.model.Organization;
import com.tjing.frame.services.DbServices;
import com.tjing.frame.util.CodeHelper;
import com.tjing.frame.util.ToolHelper;

@RestController
@RequestMapping("/public/stat")
public class StatController {

	static Logger logger = Logger.getLogger(StatController.class);

	@Autowired
	private DbServices dbServices;

	// 营销人员提成统计
	@RequestMapping("/queryActiveStat")
	public List<Object[]> queryActiveStat(HttpServletRequest request) {
		JSONObject param = CodeHelper.getRequestParams(request);

		String where = "ts.id in(select sale_id from bi_ticket where status!='0203006') and ts.strategy_id!=6";
		HashMap<Integer, Object> paramMap = new HashMap<Integer, Object>();
		String theatersn = CodeHelper.getCurrentUser().getTheaterSn();

		int i = 0;
		if (param.containsKey("createTime")) {
			where += " and date_format(ts.create_time,'%Y-%m-%d')>=?";
			paramMap.put(i++, param.getString("createTime"));
		}
		if (param.containsKey("createTime")) {
			where += " and date_format(ts.create_time,'%Y-%m-%d')<=?";
			paramMap.put(i++, param.getString("createTime2"));
		}
		if (param.containsKey("playDate")) {
			where += " and date_format(ts.play_date,'%Y-%m-%d')>=?";
			paramMap.put(i++, param.getString("playDate"));
		}
		if (param.containsKey("playDate2")) {
			where += " and date_format(ts.play_date,'%Y-%m-%d')<=?";
			paramMap.put(i++, param.getString("playDate2"));
		}
		if (param.containsKey("createUserId")&&request.getSession().getAttribute("roleString").toString().indexOf("saller_charge")==-1) {
			where += " and create_user_id=?";
			paramMap.put(i++, param.getInteger("createUserId"));
		}

		if(StringUtils.isNotBlank(theatersn)){
			where += " and ts.theater_sn=?";
			paramMap.put(i++, theatersn);
		}

		String sql = "select m.code,m.name," + "(select o.name from tj_organization o where o.id=m.dept_id) as orgName,"
				+ "(select name from tj_organization where id=m.org_id),"
				//+ "(select name from tj_user where id=ts.create_user_id),"// 操作员
				+ "sum((select sum(real_price) from bi_ticket where sale_id=ts.id and status!='0203006')) as realAmount,"// 消费金额
				+ "concat(m.commision_rate,'%'),"
				+ "sum(m.commision_rate*(select sum(real_price) from bi_ticket where sale_id=ts.id and status!='0203006')/100),"// 提成金额
				+ "sum((select count(id) from bi_ticket where sale_id=ts.id and status!='0203006')) as ticketNum"// 票数
				+ " from bi_ticket_sale ts inner join bi_salesman m on ts.saller_code=m.code where " + where
				+ " group by ts.saller_code  order by  m.code,ts.create_time";

		List<Object[]> result = dbServices.findListBySql(sql, paramMap);
		request.getSession().setAttribute("data", result);
		return result;
	}

}
